{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先 import 必要的模块\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "import lightgbm as lgbm\n",
    "from lightgbm.sklearn import LGBMClassifier\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>msno</th>\n",
       "      <th>song_id</th>\n",
       "      <th>source_system_tab</th>\n",
       "      <th>source_screen_name</th>\n",
       "      <th>source_type</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>registration_init_time</th>\n",
       "      <th>expiration_date</th>\n",
       "      <th>song_length</th>\n",
       "      <th>genre_ids</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>composer</th>\n",
       "      <th>lyricist</th>\n",
       "      <th>language</th>\n",
       "      <th>name</th>\n",
       "      <th>isrc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>V8ruy7SGk7tDm3zA51DPpn6qutt+vmKMBKa21dp54uM=</td>\n",
       "      <td>WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>20160219</td>\n",
       "      <td>20170918</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "      <td>TWUM71400047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1035059</td>\n",
       "      <td>08rvvaaab7dM7h78GC4SphLkUCSXPxpu6sY+k8aLUO4=</td>\n",
       "      <td>WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>20120105</td>\n",
       "      <td>20171113</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "      <td>TWUM71400047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>89968</td>\n",
       "      <td>1NvrMNDUcvfqOIjhim8BgdK23znMzGwAO84W+qKs6dw=</td>\n",
       "      <td>WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>14</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>20130908</td>\n",
       "      <td>20171003</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "      <td>TWUM71400047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>972394</td>\n",
       "      <td>GfSXhTVP3oj7h0545L/5xh6jD+7edQ7AH0iprl7dYbc=</td>\n",
       "      <td>WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>20131011</td>\n",
       "      <td>20170911</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "      <td>TWUM71400047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2194574</td>\n",
       "      <td>HkWEvfQyrb5Lve8X3B7HkCEkDFW8qFy/9kWFb4QbM5k=</td>\n",
       "      <td>WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>20060616</td>\n",
       "      <td>20180516</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "      <td>TWUM71400047</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        id                                          msno  \\\n",
       "0        0  V8ruy7SGk7tDm3zA51DPpn6qutt+vmKMBKa21dp54uM=   \n",
       "1  1035059  08rvvaaab7dM7h78GC4SphLkUCSXPxpu6sY+k8aLUO4=   \n",
       "2    89968  1NvrMNDUcvfqOIjhim8BgdK23znMzGwAO84W+qKs6dw=   \n",
       "3   972394  GfSXhTVP3oj7h0545L/5xh6jD+7edQ7AH0iprl7dYbc=   \n",
       "4  2194574  HkWEvfQyrb5Lve8X3B7HkCEkDFW8qFy/9kWFb4QbM5k=   \n",
       "\n",
       "                                        song_id  source_system_tab  \\\n",
       "0  WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=                  3   \n",
       "1  WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=                  3   \n",
       "2  WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=                  3   \n",
       "3  WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=                  3   \n",
       "4  WmHKgKMlp1lQMecNdNvDMkvIycZYHnFwDT72I5sIssc=                  0   \n",
       "\n",
       "   source_screen_name  source_type  city  bd  gender  registered_via  \\\n",
       "0                   8            3     1   0       2               7   \n",
       "1                   8            3     5  29       0               7   \n",
       "2                   8            3    14  20       2               3   \n",
       "3                   8            3    22  22       1               7   \n",
       "4                   4           10    15  26       0               9   \n",
       "\n",
       "   registration_init_time  expiration_date  song_length  genre_ids  \\\n",
       "0                20160219         20170918       224130        300   \n",
       "1                20120105         20171113       224130        300   \n",
       "2                20130908         20171003       224130        300   \n",
       "3                20131011         20170911       224130        300   \n",
       "4                20060616         20180516       224130        300   \n",
       "\n",
       "   artist_name  composer  lyricist  language    name          isrc  \n",
       "0        24882     33204     16297       3.0  124178  TWUM71400047  \n",
       "1        24882     33204     16297       3.0  124178  TWUM71400047  \n",
       "2        24882     33204     16297       3.0  124178  TWUM71400047  \n",
       "3        24882     33204     16297       3.0  124178  TWUM71400047  \n",
       "4        24882     33204     16297       3.0  124178  TWUM71400047  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取数据\n",
    "dpath = './data/'\n",
    "test = pd.read_csv(dpath + \"FE_test.csv\")\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>source_system_tab</th>\n",
       "      <th>source_screen_name</th>\n",
       "      <th>source_type</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>registration_init_time</th>\n",
       "      <th>expiration_date</th>\n",
       "      <th>song_length</th>\n",
       "      <th>genre_ids</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>composer</th>\n",
       "      <th>lyricist</th>\n",
       "      <th>language</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>20160219</td>\n",
       "      <td>20170918</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1035059</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>20120105</td>\n",
       "      <td>20171113</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>89968</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>14</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>20130908</td>\n",
       "      <td>20171003</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>972394</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>20131011</td>\n",
       "      <td>20170911</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2194574</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>20060616</td>\n",
       "      <td>20180516</td>\n",
       "      <td>224130</td>\n",
       "      <td>300</td>\n",
       "      <td>24882</td>\n",
       "      <td>33204</td>\n",
       "      <td>16297</td>\n",
       "      <td>3.0</td>\n",
       "      <td>124178</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        id  source_system_tab  source_screen_name  source_type  city  bd  \\\n",
       "0        0                  3                   8            3     1   0   \n",
       "1  1035059                  3                   8            3     5  29   \n",
       "2    89968                  3                   8            3    14  20   \n",
       "3   972394                  3                   8            3    22  22   \n",
       "4  2194574                  0                   4           10    15  26   \n",
       "\n",
       "   gender  registered_via  registration_init_time  expiration_date  \\\n",
       "0       2               7                20160219         20170918   \n",
       "1       0               7                20120105         20171113   \n",
       "2       2               3                20130908         20171003   \n",
       "3       1               7                20131011         20170911   \n",
       "4       0               9                20060616         20180516   \n",
       "\n",
       "   song_length  genre_ids  artist_name  composer  lyricist  language    name  \n",
       "0       224130        300        24882     33204     16297       3.0  124178  \n",
       "1       224130        300        24882     33204     16297       3.0  124178  \n",
       "2       224130        300        24882     33204     16297       3.0  124178  \n",
       "3       224130        300        24882     33204     16297       3.0  124178  \n",
       "4       224130        300        24882     33204     16297       3.0  124178  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#去掉多余的id\n",
    "test = test.drop([\"msno\",\"song_id\",\"isrc\"], axis=1)\n",
    "\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_id = test['id']   \n",
    "X_test = test.drop([\"id\"], axis=1)\n",
    "#保存特征名字以备后用（可视化）\n",
    "feat_names = X_test.columns \n",
    "from scipy.sparse import csr_matrix\n",
    "X_test = csr_matrix(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#load训练好的模型\n",
    "import pickle\n",
    "\n",
    "model = pickle.load(open(\"Music_LightGBM_.pkl\", 'rb'))\n",
    "\n",
    "#输出target的概率\n",
    "y_test_pred = model.predict_proba(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2555994, 2)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test_pred.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1\n",
      "0  0.584234\n",
      "1  0.569386\n",
      "2  0.584300\n",
      "3  0.580746\n",
      "4  0.441660\n"
     ]
    }
   ],
   "source": [
    "#生成提交结果\n",
    "out_df = pd.DataFrame(y_test_pred)\n",
    "out_df = out_df.drop([0], axis=1)\n",
    "print(out_df.head())\n",
    "\n",
    "\n",
    "columns = np.empty(1, dtype=object)\n",
    "for i in range(1):\n",
    "    columns[i] = 'target'\n",
    "    \n",
    "out_df.columns = columns\n",
    " \n",
    "out_df = pd.concat([test_id,out_df], axis = 1)\n",
    "out_df.head()\n",
    "   \n",
    "out_df.to_csv(\"submission.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
